<?php


namespace app\common\model;


use think\Hook;

class Course extends BaseModel
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = ['deletetime'];


    public function teacherCourse()
    {
        return $this->hasOne(TeacherCourse::class, 'teacher_course_id', 'teacher_course_id');
    }

    public function courseOrder()
    {
        return $this->hasOne(CourseOrder::class, 'course_order_id', 'course_order_id');
    }

    public function teacher()
    {
        return $this->belongsTo(Teacher::class, 'teacher_id', 'teacher_id')->field('teacher_id, nickname, headeravatar, phone, year');
    }

    /**
     * 课表详情
     */
    public static function getByCourseId($courseId)
    {
        $course = self::with(['teacher' => function ($query) {
            $query->withCount('course');
        }, 'courseOrder'])->find($courseId);
        if (!$course) {
            static::errorMsg(14000);
        }
        //待上课
        $course['is_start'] = self::isStart($course['date'], $course['time'], $course['status']);
        $surplus = isset($course['course_order']) ? $course['course_order']['num'] - $course['index'] : 0;
        $course['surplus'] = $surplus > 0 ? $surplus : 0;
        //已预约的课表数
        $courseOrderId = isset($course['course_order']) ? $course['course_order']['course_order_id'] : 0;
        $course['course_count'] = self::getDateCount($courseOrderId);
        $course['num'] = isset($course['course_order']) ? $course['course_order']['num'] : 0;
        return $course;
    }

    public static function getDateCount($courseOrderId)
    {
        return self::where('course_order_id', $courseOrderId)->where('date', '>', 0)->count();
    }


    /**
     * 课表列表
     */
    public static function lists($courses)
    {
        foreach ($courses as $key=>$val) {
            //授课形式
            $val['classdata'] = $val['course_order']['classdata'];
            //待上课
            $val['is_start'] = self::isStart($val['date'], $val['time'], $val['status']);
            //剩余课次
            $val['surplus'] = $val['course_order']['num'] - $val['index'] > 0 ? $val['course_order']['num'] - $val['index'] : 0;
            //预约时按顺序来让用户约课
//            if ($val['status'] == 2 && $val['index'] > self::getMinIndex($val['course_order_id'])) {
//                unset($courses[$key]);
//            }
//            unset($val['course_order']);
        }
        return $courses;
    }

    /**
     * 待上课时是否开始上课
     */
    protected static function isStart($date, $time, $status)
    {
        //待上课
        $times = explode('-', $time)[0];
        if (strtotime($date.' '.trim($times)) <= time() && $status == 3) {
            $isStart = true;
        } else {
            $isStart = false;
        }
        return $isStart;
    }

    /**
     * 课程总数
     */
    public static function getAllCourseCounts($courseOrderId)
    {
        return self::where('course_order_id', $courseOrderId)->count();
    }

    /**
     * 全部预约课程进度
     */
    public static function getAllReserve($courseOrderId)
    {
        $courses = self::where('course_order_id', $courseOrderId)->order('index asc')->select();
        array_walk($courses, function (&$val) {
            $val['is_start'] = self::isStart($val['date'], $val['time'], $val['status']);
        });
        return $courses;
    }

    /**
     * 部分预约--完成进度
     */
    public static function getOrtherFinish($courseOrderId)
    {
        $updatetimes = self::where('course_order_id', $courseOrderId)->where('status', 4)->order('updatetime desc')->column('updatetime');
        $time = [];
        $count = count($updatetimes);
        for($i = 0; $i < $count; $i++) {
            $data['time'] = date('Y-m-d H:i:s', $updatetimes[$i]);
            $data['index'] = $count - $i;
            $time[] = $data;
        }
        return [
            'time' => $time,
            'finish_count' => $count
        ];
    }

    /**
     * 课表课时
     */
    public static function getHour($time)
    {
        $times = explode('-', $time);
        $endTime = isset($times[1]) ? (intval($times[1]) ?: 24) : 0;
        $startTime = intval($times[0]);
        return $endTime - $startTime;
    }

}